package com.webank.controller;

import com.webank.bean.Record;
import com.webank.bean.ResultStatus;
import com.webank.service.ChainService;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;

@RestController
@RequestMapping("/blockchain/uploadrecord")
public class WriteBlockController {

    @Autowired
    private ChainService chainService;

    @RequestMapping(method = RequestMethod.POST)
    public ResultStatus writeBlock(@RequestBody String record) throws Exception {
        ResultStatus resultStatus = new ResultStatus();
        JSONObject jsonRecord = (JSONObject) JSONParser.parseJSON(record);
        String vin = jsonRecord.getString("vin");
        Record rec = new Record();
        rec.setVIN(vin);
        chainService.uploadRecord(rec);
        resultStatus.setStatus(ResultStatus.SUCCESS);
        resultStatus.setMsg("成功存入区块链！");
        return resultStatus;
    }
}
